package com.crm.service.impl.financeEconomicsManager;


import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.dao.mapper.financeEconomicsManager.StockBasicMapper;
import com.crm.model.entity.financeEconomicsManager.StockBasic;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

@Service
public class SinaSpiderDbServiceImpl {

    @Autowired
    private StockBasicMapper stockBasicMapper;

    private void getBasicStock(int page,String area) {
        try {
            String url = "http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page="+page+"&num=40&sort=symbol&asc=1&node="+area+"&symbol=&_s_r_a=init";
            String content = getHtml(url,"utf-8");
            System.out.println(content);

            JsonArray arry = new JsonParser().parse(content).getAsJsonArray();
            System.out.println(arry.size());

            for (JsonElement e:arry) {
                JsonObject obj = e.getAsJsonObject();
                String symbol = obj.get("symbol").getAsString();
                String  code  = obj.get("code").getAsString();
                String  name = obj.get("name").getAsString();
                System.out.println(symbol+code+name);

                StockBasic st = new StockBasic();
                st.setStockBasicId(CommonUtils.getGUID());
                st.setArea("上海");
                st.setSymbol(symbol);
                st.setCode(code);
                st.setName(name);

                int num = stockBasicMapper.insertSelective(st);
            }
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    private String getHtml(String url,String htmlCode) {
        // TODO Auto-generated method stub
        try {
            try {
                if (htmlCode == null)
                    htmlCode = "utf-8";
                if (!url.contains("http") || !url.contains("."))
                    return null;
                HttpGet get = new HttpGet(url);
                String agent = "Mozilla/5.0 (Linux; U; Android 2.3.6; zh-cn; GT-S5660 Build/GINGERBREAD)"
                        + " AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1 MicroMessenger/4.5.255";
                get.setHeader("User-Agent", agent);
                RequestConfig requestConfig = RequestConfig.custom().build();
                //get.setHeader("token","a17292f6f7039dadfc97ca1a9a1daba3");
                CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(requestConfig).build();
                HttpResponse response = client.execute(get);
                if (response == null || response.getEntity() == null)
                    return null;
                String content = EntityUtils.toString(response.getEntity(), htmlCode);
                return content;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) {
        int page = 1;
        String area = "diyu_310000";
        SinaSpiderDbServiceImpl sina = new SinaSpiderDbServiceImpl();
    }
}
